package com.example.a2032.Utils;

import android.content.Context;
import android.util.Log;

import com.amap.api.fence.GeoFence;
import com.amap.api.fence.GeoFenceClient;
import com.amap.api.fence.GeoFenceListener;
import com.amap.api.location.DPoint;
import com.hyphenate.util.LatLng;

import java.util.List;


public class GeoFenceUtils {
    public static final String GEOFENCE_BROADCAST_ACTION = "com.location.apis.geofencedemo.broadcast";
    Context context;
    GeoFenceClient mGeoFenceClient;

    public GeoFenceUtils(Context context, int geoFenceMode) {
        this.context = context;
        mGeoFenceClient = new GeoFenceClient(context);
        mGeoFenceClient.setActivateAction(geoFenceMode);
        mGeoFenceClient.createPendingIntent(GEOFENCE_BROADCAST_ACTION);
    }

    public void setGeoFenceListener(GeoFenceListener fenceListener) {
        mGeoFenceClient.setGeoFenceListener(fenceListener);
    }

    public void createGeoFence(double latitude, double longitude, float radius, String id) {
        DPoint centerPoint = new DPoint();
        centerPoint.setLatitude(latitude);
        centerPoint.setLongitude(longitude);
        mGeoFenceClient.addGeoFence(centerPoint, radius, id);
    }

    public void createGeoFence(List<LatLng> list) {
//        for (LatLng latLng : list) {
//            createGeoFence(latLng.latitude, latLng.longitude);
//        }
    }

    public void clear() {
        mGeoFenceClient.removeGeoFence();
    }

    private static final String TAG = "GeoFenceUtils";
    public void remove(String id) {
        List<GeoFence> list = mGeoFenceClient.getAllGeoFence();
        Log.i(TAG, "remove: "+list.size());
        for (GeoFence geoFence : list) {
            if (geoFence.getFenceId().equals(id)) mGeoFenceClient.removeGeoFence(geoFence);
        }

    }
}
